/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.stip.server.da;

import com.stip.server.entities.UserHub;
import com.stip.server.entities.UsersInfo;
import com.stip.server.entities.UsersMain;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityExistsException;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author BlueBerry
 */
@Stateless
@LocalBean
public class UserMainDA {
@PersistenceContext 
EntityManager em;
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public UsersMain findUsersMainById(String uid){
    UsersMain usersMain=em.find(UsersMain.class, uid);
    return  usersMain;
    }
    public String createUserMain(String uid,String password){
    UsersMain usersMain=new UsersMain();
    usersMain.setPassword(password);
    
    usersMain.setUid(uid);
    UserHub userHub=em.find(UserHub.class, uid);
    usersMain.setUserHub(userHub);
    UsersInfo usersInfo=em.find(UsersInfo.class,uid);
    usersMain.setUsersInfo(usersInfo);
    em.persist(usersMain);
        try {
            em.flush();
             return  usersMain.getUid();
        } catch (EntityExistsException e) {
            return null;
        }
    
  
    
    }
    public Boolean editUserMain(String uid,String password){
    UsersMain usersMain=em.find(UsersMain.class, uid);
        if (usersMain!=null) {
            usersMain.setPassword(password);
            return  true;
        } else {
            return  false;
        }
    
    
    }
    public Boolean deleteUserMain(String uid){
    UsersMain usersMain=em.find(UsersMain.class, uid);
        if (usersMain!=null) {
            em.remove(usersMain);
            return  true;
        } else {
            return  false;
        }
    }
}
